package sunwul.hospital.user.controller.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sunwul.hospital.common.result.Result;
import sunwul.hospital.common.utils.AuthContextHolder;
import sunwul.hospital.model.model.user.UserInfo;
import sunwul.hospital.model.vo.user.LoginVo;
import sunwul.hospital.model.vo.user.UserAuthVo;
import sunwul.hospital.user.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author sunwul
 * @date 2024/8/17 17:38:31
 * @desc
 */
@Api(tags = "用户信息")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    UserInfoService userInfoService;

    @ApiOperation(value = "用户手机号登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> info = userInfoService.loginUser(loginVo);
        return Result.ok(info);
    }

    @ApiOperation(value = "用户认证")
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //传递两个参数，第一个参数用户id，第二个参数认证数据vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request),userAuthVo);
        return Result.ok();
    }

    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        System.out.println("userId=" + userId);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }
}
